/*********************
* Author:Dzlua
* QQ:505544956
* time:2016/05/04
*/
#ifndef __EDUI_IWINDOW_H__
#define __EDUI_IWINDOW_H__

#include "../core/Msg.h"
#include "../core/Base.h"
#include "../core/TextureMgr.h"
#include "../core/AnimationMgr.h"
#include "../core/Event.h"

EDUI_NAMESPACE_BEGIN

class IEngine;
class IFont;
class IRender;
class Control;
class Container;
class ICursor;

class EDUI_API IWindow
{
    friend class IEngine;
public:
    struct {
        Event1<IWindow*> onCreate;
        Event2<IWindow*, bool*> askClose;
        Event2<Msg*, bool*> handleMsg;
        Event3<IRender*, const Rect*, bool*> onRender;
    } evt;
    virtual void Release() = 0;
protected:
    IWindow();
    virtual ~IWindow();
public:
    virtual bool Create(const char* name, const char* title, int x, int y, int w, int h, Uint32 flags, int index = -1) = 0;
    
    virtual const char* GetName() const = 0;
    virtual bool SetName(const char* name) = 0;
    virtual Uint32 GetID() const = 0;

    virtual Uint32 GetFlags() const = 0;

    virtual const char* GetTitle() const = 0;
    virtual void SetTitle(const char* title) = 0;

    virtual void SetIcon(ISurface* icon) = 0;
    virtual ISurface* GetIcon() = 0;

    //width/height is 0:auto width/height,-1:can't drag window.
    virtual bool  SetCaptionRect(const Rect* caption) = 0;
    virtual Rect GetCaptionRect() const = 0;
    virtual bool  SetSizeBox(const Rect1* rcBox) = 0;
    virtual Rect1 GetSizeBox() const = 0;

    virtual int   GetWidth() const = 0;
    virtual int   GetHeight() const = 0;
    virtual Point GetPos() const = 0;
    virtual void  SetPos(int x, int y) = 0;
    virtual Size  GetSize() const = 0;
    virtual void  SetSize(int w, int h) = 0;
    virtual Rect  GetRect() const = 0;
    virtual void  SetRect(int x, int y, int w, int h) = 0;
    virtual Size  GetMaxSize() const = 0;
    virtual void  SetMaxSize(int w, int h) = 0;
    virtual Size  GetMinSize() const = 0;
    virtual void  SetMinSize(int w, int h) = 0;

    //set user data to use,return prev data.
    virtual void* SetUserData(const char* name, void* data) = 0;
    virtual void* GetUserData(const char* name) const = 0;

    virtual bool SetFullScreen(WndFullScreen flags) = 0;

    virtual void MaxWindow() const = 0;
    virtual void MinWindow() const = 0;
    virtual void RestoreWindow() const = 0;
    virtual void ShowWindow() const = 0;
    virtual void HideWindow() const = 0;

    virtual bool IsEnabled() const = 0;
    virtual void SetEnabled(bool enabled) = 0;

    //close window and remove it from IEngine.
    virtual void Close(); 
    virtual void AskClose();
    virtual bool IsOK() = 0;
    virtual void Destroy() = 0;

    virtual IRender* GetRender() = 0;
    
    virtual IFont* GetFont() = 0;//get default font
    virtual bool SetFont(IFont* font) = 0;//set default font
    
    virtual bool SetInputFocus() = 0;
    virtual void StartTextInput() = 0;
    virtual bool IsTextInputActive() = 0;
    virtual void StopTextInput() = 0;
    virtual void SetTextInputRect(Rect* rc) = 0;

    virtual bool SetParent(IWindow* wnd) = 0;
    virtual IWindow* GetParent() const = 0;

    //0.0-1.0
    virtual bool SetOpacity(float opacity) = 0;
    //-1:err;
    virtual float GetOpacity() = 0;

    void SetCursor(ICursor* cursor);
    ICursor* GetCursor() const;

    void SetFocusCtrl(Control* ctrl);
    Control* GetFocusCtrl() const;

    void TabCtrlNext();
    void SetTabCtrl(Control* ctrl);
    Control* GetTabCtrl() const;

    //0 error
    TimerID AddTimer(Uint32 interval, TimerCallback callback, void *data1 = 0, void* data2 = 0);
    void RemoveTimer(TimerID id);

public:
    virtual void Invaldate(const Rect* rc = 0);
public:
    TextureMgr* GetTextureMgr();
    AnimationMgr* GetAnimationMgr();
    bool SetRoot(Container* container);
    Container* GetRoot() const;

    Control* FindControl(const char* name);
    Control* FindControlByTabID(int id);
    Control* FindControlByPt(int x, int y, Uint32 flag = FIND_ALL);

    ITexture*  FindTexture(const char* name, bool autoload = false);
    ITexture*  FindTexture(int id, bool autoload = false);
    Animation* FindAnimation(const char* name);
    Animation* FindAnimation(int id);

    void PushMsg(Msg *msg);
    
    // load window layout
    bool LoadLayout(const char* xmlfile);
    bool LoadLayoutFromStr(const char* xmlstr);
    // load control from xml file/string
    Control* LoadControl(const char* xmlfile);
    Control* LoadControlFromStr(const char* xmlstr);

    bool LoadAnimation(const char* xmlstr);
    bool LoadAnimation(TiXmlElement* root);

protected:
    virtual void HandleMsg(Msg* msg);
    virtual void _Render(IRender* render,const Rect* rc);
    virtual void Render(IRender* render,const Rect* rc);
    
    virtual void SetAttribute(const char* name, const char* value);
    virtual void AfterSetAttribute();
protected:
    TextureMgr m_textureMgr;
    AnimationMgr m_animationMgr;
    
    Container* m_root;
    Control* m_ctrlFocus;
    Control* m_ctrlKeyboard;
    Control* m_ctrlMouse;
    Control* m_ctrlTab;

    ICursor* m_cursor;

    Rect m_invaldate;
};

EDUI_NAMESPACE_END

#endif